package com.example.zhengchaojun.arcgis.map;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.example.zhengchaojun.arcgis.ArgisMapConstant;
import com.example.zhengchaojun.arcgis.R;

/**
 * 类描述：
 * 作者：郑朝军 on 2018/5/10
 * 邮箱：1250393285@qq.com
 * 公司：武汉智博创享科技有限公司
 * 修改人：郑朝军 on 2018/5/10
 * 修改备注：
 */
public abstract class BaseArcgisMapActivity extends AppCompatActivity
{
    protected MapView m_mapView;
    protected MapViewDelegate m_mapViewDelegate;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutID());
        init();
    }

    public abstract int getLayoutID();

    public void init()
    {
        initView();
        initData();
        initListener();
    }

    public void initView()
    {
        m_mapView = findViewById(R.id.mapView);
        m_mapViewDelegate = new MapViewManager(m_mapView);
        initMap();
    }

    private void initMap()
    {
        m_mapView.setAttributionTextVisible(false);

        initChinaMapServer();
    }

    protected void initChinaMapServer()
    {
        Basemap basemap = new Basemap(new ArcGISTiledLayer(ArgisMapConstant.ARGIS_CHINA_MAP_SERVER));
        ArcGISMap arcGISMap = new ArcGISMap(basemap);
        Point point = new Point(ArgisMapConstant.LONGITUDE, ArgisMapConstant.LATITUDE, SpatialReferences.getWgs84());
        arcGISMap.setInitialViewpoint(new Viewpoint(point, ArgisMapConstant.SCALE));
        m_mapView.setMap(arcGISMap);
    }

    public void initData()
    {
    }

    public void initListener()
    {
    }


    /**
     * 更新 地图缩小
     */
    protected void updateLevelZoomIn()
    {
        m_mapViewDelegate.updateViewpointScaleAsync(2);
    }

    /**
     * 更新地图 地图放大
     */
    protected void updateLevelZoomOut()
    {
        m_mapViewDelegate.updateViewpointScaleAsync(0.5);
    }

    @Override
    protected void onPause()
    {
        m_mapView.pause();
        super.onPause();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        m_mapView.resume();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        m_mapView.dispose();
    }
}
